package com.java46.struts2.Food.dao;


import com.java46.struts2.base.entity.TDishesEntity;
import com.java46.struts2.base.entity.TDishestypeEntity;
import com.java46.struts2.base.exception.BusinessException;
import com.java46.struts2.base.util.HibernateUtils;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import java.math.BigDecimal;
import java.util.List;
/**
 * @ClassName FoodDao
 * @Description 菜品的CRUD
 * @Author Tigger Wu
 * @Date 2018/7/16 19:33
 * @Version 1.0
 **/
public class FoodDao {
    /*
     *  查询全部菜品
     *
     * @author Tigger Wu
     * @date 2018/7/17 11:56
     * @param
     * @return
     */

    public  List<TDishesEntity> queryAll(){
        List<TDishesEntity>dlist = null;
        try {
            Query query = HibernateUtils.getQuery("from TDishesEntity");
            dlist = query.list();
        }catch (Exception e){
            throw  new BusinessException("查询失败！");
        }finally {
        }
        return dlist;
    }
    /*
     *  添加菜品
     *
     * @author Tigger Wu
     * @date 2018/7/17 11:57
     * @param
     * @return
     */
        public  void addFood(TDishesEntity foods){
        try {
            Session session = HibernateUtils.getSession();
                Transaction transaction = session.beginTransaction();
            session.save(foods);
            transaction.commit();
            session.close();
        }catch (Exception e){
            throw  new BusinessException("添加失败！");
             }
        }
/*
 * 删除菜品
 *
 * @author Tigger Wu
 * @date 2018/7/17 14:24
 * @param [food]
 * @return void
 */
//    public  void removeFoods(int id){
//                try {
//                    Session session = HibernateUtils.getSession();
//                    Transaction tran = session.beginTransaction();
//                    TDishesEntity foods = session.get(TDishesEntity.class,id);
//                    session.delete(foods);
//                    tran.commit();
//                    session.close();
//                }catch (Exception e){
//                    throw  new BusinessException("gg");
//                }
//    }
/*
 * 修改菜品
 *
 * @author Tigger Wu
 * @date 2018/7/17 14:32
 * @param
 * @return
 */
public void  updateFoods(TDishesEntity foods){
    try {
        Session session = HibernateUtils.getSession();
        Transaction tran = session.beginTransaction();
        TDishesEntity food = session.get(TDishesEntity.class,foods.getdId());
        food.setdId(foods.getdId());
        food.setdImg(foods.getdImg());
        food.setdName(foods.getdName());
        food.setdPrice(foods.getdPrice());
        food.setdRemark(foods.getdRemark());
        session.update(food);
        tran.commit();
        session.close();
    }catch (Exception e){
        throw  new BusinessException("uu");
    }

}
/**
 * 根据类型Id查询该类型所有菜品
 *
 * @author TiggerWu
 * @date 2018/7/18  10:37
 * @param
 * @return
 */
    public Integer[] queryFoodsId(int typeId){

        Integer [] arr =null;
        try {
            String hql = "from TDishesEntity f where dTypeId="+typeId;
            Session session = HibernateUtils.getSession();
            Query query = session.createQuery(hql);
            List<TDishesEntity> foods  = query.list();
            arr = new Integer[foods.size()];
            for(int i = 0 ;i<foods.size();i++){
                arr[i]= foods.get(i).getdId();

            }
        }catch (Exception e){
            throw new BusinessException("oo");
        }
        return arr;
    }
    /**
     *  根据id查询菜品
     *
     * @author TiggerWu
     * @date 2018/7/18  17:17
     * @param
     * @return
     */
    public List<TDishesEntity> queryById(int id){
        List<TDishesEntity> foods = null;
        try {
            String hql = "from TDishesEntity f where dId="+id;
            Session session = HibernateUtils.getSession();
            Query query = session.createQuery(hql);
            foods  = query.list();
            session.close();
        }catch (Exception e){
            throw new BusinessException("x",e);
        }
        return foods;
    }

    /**
     *@Author xiao
     *@Description //分页查询菜品可添加关键字
     *@Date 10:01 2018/7/18
     *@Param []
     *@return List
     */
    public static List queryFood(String Keyword,int Pageno,int Pagesize){
        String hql="";
        Query query=null;
        if(Keyword==null||Keyword.trim().equals("")){
            hql="from TDishesEntity";
        }else {
            hql="from TDishesEntity where dName like '%"+Keyword+"%'";
        }
        query=HibernateUtils.getQuery(hql).setFirstResult((Pageno-1)*Pagesize).setMaxResults(Pagesize);
        List list = query.list();
        return list;
    }

    /**
     *@Author xiao
     *@Description //查询数据总条数
     *@Date 19:27 2018/7/18
     *@Param [Keyword]
     *@return int
     */
    public static int queryFoodsCount(String Keyword){
        String hql="";
        Query query=null;
        if(Keyword==null||Keyword.trim().equals("")){
            hql="select count(*) from TDishesEntity";
        }else {
            hql="select count(*) from TDishesEntity where dName like '%"+Keyword+"%'";
        }
        query=HibernateUtils.getQuery(hql);
        Object o = query.uniqueResult();
        return Integer.parseInt(o.toString());
    }

    /**
     *@Author xiao
     *@Description //根据菜品编号删除菜品
     *@Date 15:04 2018/7/18
     *@Param [orderid]
     *@return void
     */
    public static void deleteFood(int foodid){
        Session session=HibernateUtils.getSession();
        Transaction transaction = session.beginTransaction();
        String hql="delete from TDishesEntity where dId=?1";
        Query query=session.createQuery(hql);
        query.setParameter(1,foodid);
        query.executeUpdate();
        transaction.commit();
        session.close();
    }


//    /**
//     * @Author xiaobo
//     * @Description //添加菜品类型
//     * @Date 17:01 2018/7/22 0022
//     * @Param [Typename]
//     * @return void
//     */
//    public static void addType(String Typename){
//        Session session=HibernateUtils.getSession();
//        Transaction transaction = session.beginTransaction();
//        TDishestypeEntity tDishestypeEntity = new TDishestypeEntity();
//        tDishestypeEntity.setdTypeName(Typename);
//        tDishestypeEntity.setdTypeId(0);
//        session.save(tDishestypeEntity);
//        transaction.commit();
//        session.close();
//    }
 }

